package com.alibaba.algo.controller.user;

import com.alibaba.algo.annotation.OperateLogAnnotation;
import com.alibaba.algo.apiimpl.user.SysOrgLabelServiceImpl;
import com.alibaba.algo.cooperation.common.BaseResult;
import com.alibaba.algo.cooperation.usercenter.GlobalResultStatus;
import com.alibaba.algo.cooperation.usercomponent.vo.req.user.OrgLabelEmployeeRequestVO;
import com.alibaba.algo.cooperation.usercomponent.vo.req.user.OrgLabelPageVO;
import com.alibaba.algo.enums.OperateTypeEnum;
import com.alibaba.algo.enums.ResourceTypeEnum;
import com.xxl.sso.core.conf.Conf;
import com.xxl.sso.core.user.XxlSsoUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 岗位标签相关接口类
 *
 * @author Songtinghua
 * @create 2021-10-29 11:00
 **/
@RestController
@RequestMapping(value = "/usercenter-admin-app/admin/sysLabel")
@Api(value = "岗位标签", tags = "岗位标签")
public class SysOrgLabelController {

    @Autowired
    private SysOrgLabelServiceImpl sysOrgLabelService;


    /**
     * 新增岗位标签
     *
     * @param param web input param
     * @return BeanResult
     */
    @PostMapping("/insert")
    @ApiOperation(value = "岗位标签新增")
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.LABEL,operateType = OperateTypeEnum.INSERT)
    public BaseResult insert(@RequestBody OrgLabelEmployeeRequestVO param, HttpServletRequest request) {
        if (null == param) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_ERROR.getCode(), GlobalResultStatus.PARAM_ERROR.getMsg());
        }
        if (StringUtils.isEmpty(param.getLabelCode())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_ORG_LABEL_CODE_ERROR.getCode(), GlobalResultStatus.PARAM_ORG_LABEL_CODE_ERROR.getMsg());
        }
        if (StringUtils.isEmpty(param.getLabelName())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_ORG_LABEL_NAME_ERROR.getCode(), GlobalResultStatus.PARAM_ORG_LABEL_NAME_ERROR.getMsg());
        }
        XxlSsoUser xxlSsoUser = (XxlSsoUser) request.getAttribute(Conf.SSO_USER);
        if(null != xxlSsoUser) {
            param.setUserName(xxlSsoUser.getUsername());
        }
        return sysOrgLabelService.insert(param);
    }

    /**
     * 新增岗位标签
     *
     * @param param web input param
     * @return BeanResult
     */
    @PostMapping("/update")
    @ApiOperation(value = "岗位标签更新")
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.LABEL,operateType = OperateTypeEnum.UPDATE)
    public BaseResult update(@RequestBody OrgLabelEmployeeRequestVO param, HttpServletRequest request) {
        if (null == param.getId()) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_ORG_LABEL_DELETE_ID_ERROR.getCode(), GlobalResultStatus.PARAM_ORG_LABEL_DELETE_ID_ERROR.getMsg());
        }
        XxlSsoUser xxlSsoUser = (XxlSsoUser) request.getAttribute(Conf.SSO_USER);
        if(null != xxlSsoUser) {
            param.setUserName(xxlSsoUser.getUsername());
        }
        return sysOrgLabelService.update(param);
    }

    /**
     * 岗位标签批量删除
     *
     * @param ids ids that need delete data
     * @return BeanResult
     */
    @PostMapping("/delete")
    @ApiOperation(value = "岗位标签批量删除")
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.LABEL,operateType = OperateTypeEnum.DELETE)
    public BaseResult delete(@RequestBody List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_ORG_LABEL_DELETE_ID_ERROR.getCode(), GlobalResultStatus.PARAM_ORG_LABEL_DELETE_ID_ERROR.getMsg());
        }
        return sysOrgLabelService.delete(ids);
    }

    /**
     * 新增岗位查询详情
     *
     * @param id table`s id
     * @return BeanResult
     */
    @PostMapping("/selectDetail")
    @ApiOperation(value = "岗位标签详情查询")
    public BaseResult selectDetail(@RequestParam Long id) {
        if (null == id) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_ORG_LABEL_DELETE_ID_ERROR.getCode(), GlobalResultStatus.PARAM_ORG_LABEL_DELETE_ID_ERROR.getMsg());
        }
        return sysOrgLabelService.selectDetail(id);
    }

    /**
     * 新增岗位列表查询
     *
     * @param param web input param
     * @return BeanResult
     */
    @PostMapping("/selectPage")
    @ApiOperation(value = "岗位标签列表查询")
    public BaseResult selectPage(@RequestBody OrgLabelPageVO param) {
        return sysOrgLabelService.selectPage(param);
    }
}
